package com.user.aspect;

import com.common.utils.TimeUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.CodeSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author Elias
 * @version 1.0
 * @date 2021-06-03 9:11
 * @description
 * @copyright 2021
 */
@Aspect
@Component
public class LoginAspect {
  @Autowired private StringRedisTemplate stringRedisTemplate;
    /**
     * 实现时长续租功能
     * @param point 连接点
     * @param result 切入点返回值
     */
  @AfterReturning(
      value = "execution(* com.user.service.UserService*.loginState(..))",
      returning = "result")
  public void extendTime(JoinPoint point, Object result) {
    if (StringUtils.isEmpty(result)) {
      return;
    }
    Map<String, Object> map = new HashMap<>();
    Object[] values = point.getArgs();
    String[] names = ((CodeSignature) point.getSignature()).getParameterNames();
    for (int i = 0; i < names.length; i++) {
      map.put(names[i], values[i]);
    }
    String ticket = (String) map.get("ticket");
    String key = TimeUtils.cutTimeStamp(ticket);
    stringRedisTemplate.expire(key, 30, TimeUnit.MINUTES);
  }
}
